package com.example.handleuser;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	TextView QuesTV = null;
	TextView Opt1TV = null;
	TextView Opt2TV = null;
	TextView Opt3TV = null;
	TextView Opt4TV = null;
	TextView Streak = null;
	TextView Ans = null;
	TextView Atm = null;
	TextView Points = null;
	TextView QPoints = null;
	TextView StreakHead = null;
	TextView ansHead = null;
	TextView atmHead = null;
	TextView upts = null;
	TextView qpts = null;
	Questions[] ques = new Questions[5];
	Questions q = new Questions();
	Users user = new Users();
	static int streak;
	static int attempted;
	static int answered;
	static int user_points;
	static int q_points;
	static int j, i;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		i = 0;
		j = 0;
		user.setAnswered(10);
		user.setAttempted(35);
		user.setStreak(4);

		displayQuestion(0);

	}

	public void displayQuestion(int i) {
		if (i < 11) {
			q = setQues(i);

			QuesTV = (TextView) findViewById(R.id.questionTextView);
			Opt1TV = (TextView) findViewById(R.id.Option1TextView);
			Opt2TV = (TextView) findViewById(R.id.Option2TextView);
			Opt3TV = (TextView) findViewById(R.id.Option3TextView);
			Opt4TV = (TextView) findViewById(R.id.Option4TextView);

			Streak = (TextView) findViewById(R.id.streakTextView);
			Ans = (TextView) findViewById(R.id.answeredTextView);
			Atm = (TextView) findViewById(R.id.attemptedTextView);
			Points = (TextView) findViewById(R.id.userPointsTextView);
			QPoints = (TextView) findViewById(R.id.qPTextView);

			QuesTV.setText(q.getQuestion());
			Opt1TV.setText(q.getOption1());
			Opt2TV.setText(q.getOption2());
			Opt3TV.setText(q.getOption3());
			Opt4TV.setText(q.getOption4());

			streak = user.getStreak();
			attempted = user.getAttempted();
			answered = user.getAnswered();
			user_points = user.getCoins();
			q_points = getQuestionPoints();
			
			Log.i("displayQuestion(): q_points=", "" + q_points);

			Streak.setText("" + streak);
			Ans.setText("" + answered);
			Atm.setText("" + attempted);
			Points.setText(""+user_points);
			QPoints.setText(""+q_points);

			Opt1TV.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (Opt1TV.getText().toString()
							.equalsIgnoreCase(q.getAnswer())) {
						if (((streak % 5) == 0) && (streak!=0)) {
							streak++;
							attempted++;
							answered++;
							user_points += q_points + 5;
							//q_points+=1;
							//q.setQuestion_points(q_points);

							Toast.makeText(getApplicationContext(),
									"Congrats!!! BONUS Earned",
									Toast.LENGTH_SHORT).show();
						} else {
							streak++;
							attempted++;
							answered++;
							user_points += q_points;
							
							Toast.makeText(getApplicationContext(), "Correct",
									1000).show();
						}
						
					} else {

						streak = 0;
						attempted++;

						Toast.makeText(getApplicationContext(),
								"Wrong; Answer: " + q.getAnswer(),
								1000).show();
					}

					Log.i("onClick(): j=", "" + j);
					Log.i("onClick(): streak=", "" + streak);
					Log.i("onClick(): answered=", "" + answered);
					Log.i("onClick(): attempted=", "" + attempted);
					Log.i("onClick(): user_points=", "" + user_points);
					Log.i("onClick(): q_points=", "" + q_points);

					user.setStreak(streak);
					user.setAnswered(answered);
					user.setAttempted(attempted);
					user.setCoins(user_points);
					setQuestionPoints(streak);

					displayQuestion(++j);
				}

			});

			Opt2TV.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (Opt2TV.getText().toString()
							.equalsIgnoreCase(q.getAnswer())) {
						if (((streak % 5) == 0) && (streak!=0)) {
							streak++;
							attempted++;
							answered++;
							user_points += q_points + 5;
							//q_points+=1;

							Toast.makeText(getApplicationContext(),
									"Congrats!!! BONUS Earned",
									Toast.LENGTH_SHORT).show();
						} else {
							streak++;
							attempted++;
							answered++;
							user_points += q_points;
							Toast.makeText(getApplicationContext(), "Correct",
									1000).show();
						}

					} else {
						streak = 0;
						attempted++;

						Toast.makeText(getApplicationContext(),
								"Wrong; Answer: " + q.getAnswer(),
								1000).show();
					}
					Log.i("onClick(): j=", "" + j);
					Log.i("onClick(): streak=", "" + streak);
					Log.i("onClick(): answered=", "" + answered);
					Log.i("onClick(): attempted=", "" + attempted);
					Log.i("onClick(): user_points=", "" + user_points);
					Log.i("onClick(): q_points=", "" + q_points);

					user.setStreak(streak);
					user.setAnswered(answered);
					user.setAttempted(attempted);
					user.setCoins(user_points);
					setQuestionPoints(streak);

					displayQuestion(++j);

				}

			});

			Opt3TV.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (Opt3TV.getText().toString()
							.equalsIgnoreCase(q.getAnswer())) {
						if (((streak % 5) == 0) && (streak!=0)) {
							streak++;
							attempted++;
							answered++;
							user_points += q_points + 5;
							//q_points+=1;

							Toast.makeText(getApplicationContext(),
									"Congrats!!! BONUS Earned",
									Toast.LENGTH_SHORT).show();
						} else {
							streak++;
							attempted++;
							answered++;
							user_points += q_points;
							
							Toast.makeText(getApplicationContext(), "Correct",
									1000).show();
						}

						
					} else {

						streak = 0;
						attempted++;

						Toast.makeText(getApplicationContext(),
								"Wrong; Answer: " + q.getAnswer(),
								1000).show();
					}
					Log.i("onClick(): j=", "" + j);
					Log.i("onClick(): streak=", "" + streak);
					Log.i("onClick(): answered=", "" + answered);
					Log.i("onClick(): attempted=", "" + attempted);
					Log.i("onClick(): user_points=", "" + user_points);
					Log.i("onClick(): q_points=", "" + q_points);

					user.setStreak(streak);
					user.setAnswered(answered);
					user.setAttempted(attempted);
					user.setCoins(user_points);
					setQuestionPoints(streak);

					displayQuestion(++j);
				}

			});

			Opt4TV.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (Opt4TV.getText().toString()
							.equalsIgnoreCase(q.getAnswer())) {
						if (((streak % 5) == 0) && (streak!=0)) {
							streak++;
							attempted++;
							answered++;
							user_points += q_points + 5;
							q_points+=1;
							
							Toast.makeText(getApplicationContext(),
									"Congrats!!! BONUS Earned",
									Toast.LENGTH_SHORT).show();
						} else {
							streak++;
							attempted++;
							answered++;
							user_points += q_points;
							
							Toast.makeText(getApplicationContext(), "Correct",
									1000).show();
						}

					} else {
						streak = 0;
						attempted++;

						Toast.makeText(getApplicationContext(),
								"Wrong; Answer: " + q.getAnswer(),
								1000).show();
					}
					Log.i("onClick(): j=", "" + j);
					Log.i("onClick(): streak=", "" + streak);
					Log.i("onClick(): answered=", "" + answered);
					Log.i("onClick(): attempted=", "" + attempted);
					Log.i("onClick(): user_points=", "" + user_points);
					Log.i("onClick(): q_points=", "" + q_points);

					user.setStreak(streak);
					user.setAnswered(answered);
					user.setAttempted(attempted);
					user.setCoins(user_points);
					setQuestionPoints(streak);
					
					displayQuestion(++j);
				}

			});

		} else {
			finish();
		}
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
	}

	public Questions setQues(int i) {

		Questions q = new Questions();
		Questions ques[] = new Questions[11];

		ques[0] = new Questions();
		ques[0].setQuestion("What is the capital of India?");
		ques[0].setOption1("New Delhi");
		ques[0].setOption2("New Mexico");
		ques[0].setOption3("New York");
		ques[0].setOption4("New Tokyo");
		ques[0].setAnswer("New Delhi");

		ques[1] = new Questions();
		ques[1].setQuestion("What is the capital of Russia?");
		ques[1].setOption1("New Delhi");
		ques[1].setOption2("Moscow");
		ques[1].setOption3("New York");
		ques[1].setOption4("New Tokyo");
		ques[1].setAnswer("Moscow");
		ques[2] = new Questions();

		ques[2].setQuestion("What is the capital of Brazil?");
		ques[2].setOption1("New Delhi");
		ques[2].setOption2("New Mexico");
		ques[2].setOption3("Brazillia");
		ques[2].setOption4("New Tokyo");
		ques[2].setAnswer("Brazillia");

		ques[3] = new Questions();
		ques[3].setQuestion("What is the capital of China?");
		ques[3].setOption1("New Delhi");
		ques[3].setOption2("New Mexico");
		ques[3].setOption3("New York");
		ques[3].setOption4("Bejing");
		ques[3].setAnswer("Bejing");

		ques[4] = new Questions();
		ques[4].setQuestion("What is the capital of Japan?");
		ques[4].setOption1("New Delhi");
		ques[4].setOption2("New Mexico");
		ques[4].setOption3("New York");
		ques[4].setOption4("Tokyo");
		ques[4].setAnswer("Tokyo");

		ques[5] = new Questions();
		ques[5].setQuestion("What is the capital of Pakistan?");
		ques[5].setOption1("New Delhi");
		ques[5].setOption2("Islamabad");
		ques[5].setOption3("New York");
		ques[5].setOption4("Tokyo");
		ques[5].setAnswer("Islamabad");
		
		ques[6] = new Questions();
		ques[6].setQuestion("What is the capital of Bangladesh?");
		ques[6].setOption1("Dhaka");
		ques[6].setOption2("New Mexico");
		ques[6].setOption3("New York");
		ques[6].setOption4("Tokyo");
		ques[6].setAnswer("Dhaka");
		
		ques[7] = new Questions();
		ques[7].setQuestion("What is the capital of Argentina?");
		ques[7].setOption1("New Delhi");
		ques[7].setOption2("New Mexico");
		ques[7].setOption3("Bunos Aires");
		ques[7].setOption4("Tokyo");
		ques[7].setAnswer("Bunos Aires");
		
		ques[8] = new Questions();
		ques[8].setQuestion("What is the capital of Mexico?");
		ques[8].setOption1("New Delhi");
		ques[8].setOption2("New Mexico");
		ques[8].setOption3("New York");
		ques[8].setOption4("Mexico City");
		ques[8].setAnswer("Mexico City");

		ques[9] = new Questions();
		ques[9].setQuestion("What is the capital of Australia?");
		ques[9].setOption1("Canberra");
		ques[9].setOption2("New Mexico");
		ques[9].setOption3("New York");
		ques[9].setOption4("Mexico City");
		ques[9].setAnswer("Canberra");
		
		ques[10] = new Questions();
		ques[10].setQuestion("What is the capital of Canada?");
		ques[10].setOption1("Canberra");
		ques[10].setOption2("New Mexico");
		ques[10].setOption3("Toronto");
		ques[10].setOption4("Mexico City");
		ques[10].setAnswer("Toronto");
		
		return ques[i];
	}

	public Users setUser(int streak, int atm, int ans, int points) {
		Users u = new Users();
		u.setStreak(streak);
		u.setAttempted(atm);
		u.setAnswered(ans);
		u.setCoins(points);

		return u;
	}
	
	void setQuestionPoints(int streak){
		int que_points=1;
		if(streak!=0){
			que_points=(streak/7)+1;
		}else{
			que_points=1;
		}
		this.q_points=que_points;
	}
	
	int getQuestionPoints(){
		return this.q_points;
	}


}

/*
 * void junk(){ TextView QuesTV = null; TextView Opt1TV = null; TextView Opt2TV
 * = null; TextView Opt3TV = null; TextView Opt4TV = null; TextView Streak =
 * null; TextView Ans = null; TextView Atm = null; Questions[] ques = new
 * Questions[5]; Questions q = new Questions(); Users user = new Users(); int
 * streak=user.getStreak(); int attempted=user.getAttempted(); int
 * answered=user.getAnswered(); int user_points=user.getCoins(); int q_points=1;
 * String answer;
 * 
 * //@Override //protected void onCreate(Bundle savedInstanceState) {
 * //super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
 * 
 * QuesTV = (TextView) findViewById(R.id.questionTextView); Opt1TV = (TextView)
 * findViewById(R.id.Option1TextView); Opt2TV = (TextView)
 * findViewById(R.id.Option2TextView); Opt3TV = (TextView)
 * findViewById(R.id.Option3TextView); Opt4TV = (TextView)
 * findViewById(R.id.Option4TextView); Streak = (TextView)
 * findViewById(R.id.streakTextView); Ans = (TextView)
 * findViewById(R.id.answeredTextView); Atm = (TextView)
 * findViewById(R.id.attemptedTextView);
 * 
 * user = setUser(streak, attempted, answered, user_points); //int i=4; for (int
 * i = 0; i < ques.length; i++) { ques[i] = setQues(i); q = ques[i];
 * 
 * QuesTV.setText(q.getQuestion()); Opt1TV.setText(q.getOption1());
 * Opt2TV.setText(q.getOption2()); Opt3TV.setText(q.getOption3());
 * Opt4TV.setText(q.getOption4()); Streak.setText("" + user.getStreak());
 * Ans.setText("" + user.getAnswered()); Atm.setText("" + user.getAttempted());
 * 
 * streak = user.getStreak(); attempted = user.getAttempted(); answered =
 * user.getAnswered(); user_points = user.getCoins(); q_points =
 * q.getQuestion_points();
 * 
 * Opt1TV.setOnClickListener(new OnClickListener() {
 * 
 * @Override public void onClick(View v) {
 * 
 * if (Opt1TV.getText().toString() .equalsIgnoreCase(q.getAnswer())) {
 * Toast.makeText(getApplicationContext(), "Correct",
 * Toast.LENGTH_SHORT).show(); streak++; attempted++; answered++; user_points +=
 * q_points;
 * 
 * } else {
 * 
 * Toast.makeText(getApplicationContext(), "Wrong; Answer" + q.getAnswer(),
 * Toast.LENGTH_SHORT).show(); streak = 0; attempted++; }
 * 
 * Toast.makeText(getApplicationContext(), "Data Updated",
 * Toast.LENGTH_SHORT).show(); }
 * 
 * });
 * 
 * Opt2TV.setOnClickListener(new OnClickListener() {
 * 
 * @Override public void onClick(View v) { if (Opt2TV.getText().toString()
 * .equalsIgnoreCase(q.getAnswer())) { Toast.makeText(getApplicationContext(),
 * "Correct", Toast.LENGTH_SHORT).show(); streak++; attempted++; answered++;
 * user_points += q_points; } else { Toast.makeText(getApplicationContext(),
 * "Wrong; Answer" + q.getAnswer(), Toast.LENGTH_SHORT).show(); streak = 0;
 * attempted++; } Toast.makeText(getApplicationContext(), "Data Updated",
 * Toast.LENGTH_SHORT).show();
 * 
 * }
 * 
 * });
 * 
 * Opt3TV.setOnClickListener(new OnClickListener() {
 * 
 * @Override public void onClick(View v) {
 * 
 * // if(Opt3TV.getText().toString().equalsIgnoreCase(answer)){ if
 * (Opt3TV.getText().toString() .equalsIgnoreCase(q.getAnswer())) {
 * Toast.makeText(getApplicationContext(), "Correct",
 * Toast.LENGTH_SHORT).show(); streak++; attempted++; answered++; user_points +=
 * q_points; } else { Toast.makeText(getApplicationContext(), "Wrong; Answer" +
 * q.getAnswer(), Toast.LENGTH_SHORT).show(); streak = 0; attempted++; }
 * Toast.makeText(getApplicationContext(), "Data Updated",
 * Toast.LENGTH_SHORT).show(); }
 * 
 * });
 * 
 * Opt4TV.setOnClickListener(new OnClickListener() {
 * 
 * @Override public void onClick(View v) {
 * 
 * // if(Opt4TV.getText().toString().equalsIgnoreCase(answer)){ if
 * (Opt4TV.getText().toString() .equalsIgnoreCase(q.getAnswer())) {
 * Toast.makeText(getApplicationContext(), "Correct",
 * Toast.LENGTH_SHORT).show(); streak++; attempted++; answered++; user_points +=
 * q_points; } else { Toast.makeText(getApplicationContext(), "Wrong; Answer" +
 * q.getAnswer(), Toast.LENGTH_SHORT).show(); streak = 0; attempted++; }
 * Toast.makeText(getApplicationContext(), "Data Updated",
 * Toast.LENGTH_SHORT).show(); }
 * 
 * });
 * 
 * ques[i] = q; ques[i].setQuestion_points(q_points);
 * user.setAnswered(answered); user.setAttempted(attempted);
 * user.setCoins(user_points); user.setStreak(streak); Streak.setText("" +
 * user.getStreak()); Ans.setText("" + user.getAnswered()); Atm.setText("" +
 * user.getAttempted());
 * 
 * 
 * }
 * 
 * 
 * }
 * 
 * public Questions setQues(int i) {
 * 
 * Questions q = new Questions(); Questions ques[] = new Questions[5];
 * 
 * 
 * q.setQuestion("What is the capital of India?"); q.setOption1("New Delhi");
 * q.setOption2("New Mexico"); q.setOption3("New York");
 * q.setOption4("New Tokyo"); q.setAnswer("New Delhi");
 * 
 * 
 * ques[0]=new Questions();
 * ques[0].setQuestion("What is the capital of India?");
 * ques[0].setOption1("New Delhi"); ques[0].setOption2("New Mexico");
 * ques[0].setOption3("New York"); ques[0].setOption4("New Tokyo");
 * ques[0].setAnswer("New Delhi");
 * 
 * ques[1]=new Questions();
 * ques[1].setQuestion("What is the capital of Russia?");
 * ques[1].setOption1("New Delhi"); ques[1].setOption2("Moscow");
 * ques[1].setOption3("New York"); ques[1].setOption4("New Tokyo");
 * ques[1].setAnswer("Moscow");
 * 
 * ques[2]=new Questions();
 * ques[2].setQuestion("What is the capital of Brazil?");
 * ques[2].setOption1("New Delhi"); ques[2].setOption2("New Mexico");
 * ques[2].setOption3("Brazillia"); ques[2].setOption4("New Tokyo");
 * ques[2].setAnswer("Brazillia");
 * 
 * ques[3]=new Questions();
 * ques[3].setQuestion("What is the capital of China?");
 * ques[3].setOption1("New Delhi"); ques[3].setOption2("New Mexico");
 * ques[3].setOption3("New York"); ques[3].setOption4("Bejing");
 * ques[3].setAnswer("Bejing");
 * 
 * ques[4]=new Questions();
 * ques[4].setQuestion("What is the capital of Japan?");
 * ques[4].setOption1("New Delhi"); ques[4].setOption2("New Mexico");
 * ques[4].setOption3("New York"); ques[4].setOption4("Tokyo");
 * ques[4].setAnswer("Tokyo");
 * 
 * 
 * for (int j = 0; j < ques.length; j++) { if (i == 0) { q = ques[0]; } }
 * 
 * return q; }
 * 
 * }
 */